package com.controller;

import com.dto.Result;
import com.entity.PropertyProjectInfo;
import com.service.PropertyProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/property/project")
public class PropertyProjectInfoController {

    @Autowired
    private PropertyProjectInfoService propertyProjectInfoService;

    @PostMapping
    public void create(@RequestBody PropertyProjectInfo propertyProjectInfo) {
        propertyProjectInfoService.save(propertyProjectInfo);

    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        propertyProjectInfoService.deleteById(id);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable Integer id, @RequestBody PropertyProjectInfo propertyProjectInfo) {
        propertyProjectInfo.setId(id);
        propertyProjectInfoService.update(propertyProjectInfo);

    }

    @GetMapping("/{id}")
    public PropertyProjectInfo detail(@PathVariable Integer id) {
        return propertyProjectInfoService.findById(id);
    }

    @GetMapping("/list")
    public List<PropertyProjectInfo> list() {
        return propertyProjectInfoService.findAll();
    }

    @GetMapping("/page")
    public Page<PropertyProjectInfo> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String projectName) {
        
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<PropertyProjectInfo> page;
        
        if (projectName != null && !projectName.isEmpty()) {
            page = propertyProjectInfoService.findByProjectNameContaining(projectName, pageable);
        } else {
            page = propertyProjectInfoService.findAll(pageable);
        }
        
        return page;
    }
}